﻿package net.wangit.app.user;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import net.wangit.Invocation;
import net.wangit.context.ActionContext;
import net.wangit.support.ActionInterceptor;


public class OutFileAI implements ActionInterceptor {
	private static final int BUFFER_SIZE = 1024;

	public void intercept(ActionContext ac, Invocation invocation)
			throws Exception {
		File file = ac.getFile("user.personal.photo");
		String fileName = ac.getRequestParameter("user.personal.photofilename");
		File dst = new File("d:/photo/" + fileName);
		InputStream in = new BufferedInputStream(new FileInputStream(file),
				BUFFER_SIZE);
		OutputStream out = new BufferedOutputStream(new FileOutputStream(dst),
				BUFFER_SIZE);
		byte[] buffer = new byte[BUFFER_SIZE];
		int byteCount = in.read(buffer);
		while (byteCount > 0) {
			out.write(buffer, 0, byteCount);
			byteCount = in.read(buffer);
		}
		in.close();
		out.close();
		invocation.invoke();
	}
}
